﻿namespace ScrumLite.WebUx.Tests
{
    using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ScrumLite.Services.Contracts;
    using ScrumLite.WebUx.Controllers;
    using ScrumLite.Services;
    using ScrumLite.WebUx.Models;
    using System.Globalization;
    using System.Web.Mvc;

    /// <summary>
    /// Summary description for DeliverablesControllerFixture
    /// </summary>
    [TestClass]
    public class DeliverablesControllerFixture
    {
        public DeliverablesControllerFixture()
        {         
        }

        [TestMethod]
        public void ShouldAddDeliverable()
        {
            Deliverable mockDeliverable = MockHelpers.DeliverableMock;
            
            MockDeliverablesService service = new MockDeliverablesService();
            TestableDeliverablesController controller = new TestableDeliverablesController(service);
            MockHelpers.SetupMocks(controller);

            controller.Request.Form["chkWorkAreas"] = "AA184E41-B84D-4c2f-9251-CB0F8E8D7DDB";

            controller.Save("AF06A611-12CE-4C06-B3AD-E2754D12D516", string.Empty, mockDeliverable.ReleasedOn, mockDeliverable.Name, mockDeliverable.Url.ToString());
            
            Assert.IsTrue(service.AddDeliverableCalled);
        }

        [TestMethod]
        public void ShouldUpdateDeliverable()
        {
            MockDeliverablesService service = new MockDeliverablesService();
            TestableDeliverablesController controller = new TestableDeliverablesController(service);
            MockHelpers.SetupMocks(controller);

            Deliverable deliverable = service.GetDeliverable("A06A8A24-7DAF-408A-A0B1-3D63E5D42F29");
            
            controller.Request.Form["chkWorkAreas"] = "AA184E41-B84D-4c2f-9251-CB0F8E8D7DDB";

            controller.Save("AF06A611-12CE-4C06-B3AD-E2754D12D516", deliverable.Id.ToString(), deliverable.ReleasedOn, deliverable.Name, deliverable.Url.ToString());

            Assert.IsTrue(service.UpdateDeliverableCalled);
            Assert.IsNotNull(service.DeliverableUpdated);
            Assert.AreEqual(service.DeliverableUpdated.Id, deliverable.Id);
            Assert.AreEqual(service.DeliverableUpdated.Name, deliverable.Name);
            Assert.AreEqual(service.DeliverableUpdated.ReleasedOn, deliverable.ReleasedOn);
            Assert.AreEqual(service.DeliverableUpdated.Url, deliverable.Url);
        }

        [TestMethod]
        public void ShouldGetDeliverable()
        {
            MockDeliverablesService service = new MockDeliverablesService();
            TestableDeliverablesController controller = new TestableDeliverablesController(service);

            Deliverable result = controller.Get("A06A8A24-7DAF-408A-A0B1-3D63E5D42F29");
            
            Assert.IsNotNull(result);
            Assert.AreEqual(result.Id, new Guid("A06A8A24-7DAF-408A-A0B1-3D63E5D42F29")); 
        }

        [TestMethod]
        public void ShouldGetDeliverableList()
        {
            MockDeliverablesService service = new MockDeliverablesService();
            TestableDeliverablesController controller = new TestableDeliverablesController(service);
            MockHelpers.SetupMocks(controller);

            controller.Request.Form["chkWorkAreas"] = "AA184E41-B84D-4C2F-9251-CB0F8E8D7DDB";
            controller.List("AF06A611-12CE-4C06-B3AD-E2754D12D516");
            Assert.IsNotNull(controller.Data);
            
        }

        [TestMethod]
        public void ShouldControllerPassViewDataToWriteViewOnEdit()
        {
            MockDeliverablesService service = new MockDeliverablesService();
            TestableDeliverablesController controller = new TestableDeliverablesController(service);
            MockHelpers.SetupMocks(controller);

            controller.Edit("AF06A611-12CE-4C06-B3AD-E2754D12D516", "A06A8A24-7DAF-408A-A0B1-3D63E5D42F29");

            Assert.IsNotNull(controller.Data);
            Assert.IsInstanceOfType(controller.Data, typeof(DeliverableViewData));
            Assert.AreEqual("A06A8A24-7DAF-408A-A0B1-3D63E5D42F29", ((DeliverableViewData)controller.Data).Id.ToString().ToUpper(CultureInfo.CurrentCulture));
        }

    }

    class TestableDeliverablesController : DeliverablesController
    {
        public object Data;
        
        public TestableDeliverablesController(IDeliverablesService service)
            : base(service, new MockProjectsService(), new MockRequestsService(), new MockProfileService())
        {
        }

        protected override void RenderView(string viewName, string masterName, object viewData)
        {
            Data = viewData;
        }                       
    }
}
